package com.binc.testspring.common.utils;

import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class Global extends AbstractConfiguration {
    private static Map<String, String> RESPONSE_CODE_PREFIX_MAPPED = new HashMap();

    public Global() {
    }

    public static HttpServletRequest getCurrentRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static DeviceType getRemoteDeviceType() {
        String userAgent = getUserAgent();
        return getRemoteDeviceTypeByUserAgent(userAgent);
    }

    public static DeviceType getRemoteDeviceTypeByUserAgent(String userAgent) {
        if (StringUtils.isEmpty(userAgent)) {
            return DeviceType.PC_WEB;
        } else if (userAgent.contains("MicroMessenger")) {
            return DeviceType.OTHER_TERMINAL;
        } else {
            return !userAgent.contains("iPhone") && !userAgent.contains("iPad") && !userAgent.contains("iOS") && !userAgent.contains("Android") ? DeviceType.PC_WEB : DeviceType.MOBILE;
        }
    }

    public static boolean isBrowser() {
        DeviceType remoteDeviceType = getRemoteDeviceType();
        return remoteDeviceType == DeviceType.PC_WEB;
    }

    public static boolean isAppEndpoint() {
        DeviceType remoteDeviceType = getRemoteDeviceType();
        return remoteDeviceType == DeviceType.MOBILE;
    }

    public static boolean isApplets() {
        DeviceType remoteDeviceType = getRemoteDeviceType();
        return remoteDeviceType == DeviceType.OTHER_TERMINAL;
    }

    public static String getUserAgent() {
        return getCurrentRequest().getHeader("user-agent");
    }

    public static <T> List<T> removeDuplicateCase(List<T> cases, Comparator<T> comparator) {
        Set<T> set = new TreeSet(comparator);
        set.addAll(cases);
        return new ArrayList(set);
    }

    // public static Long generateId(Class clazz) {
    //     return CommonIDGenerator.generate();
    // }

    // public static String buildResponseCode(String httpStatusCode, String businessCode) {
    //     StringBuffer code = new StringBuffer();
    //     String appKey = RuntimeUtils.getAppKey();
    //     String willow = Constants.WILLOW_RESPONSE_CODE_PREFIX + appKey;
    //     String systemCode = Willow.getStringValue(willow, "");
    //     if (StringUtils.isEmpty(systemCode)) {
    //         systemCode = (String)RESPONSE_CODE_PREFIX_MAPPED.get(appKey);
    //     }
    //
    //     if (StringUtils.isEmpty(systemCode)) {
    //         throw new RuntimeException(String.format("缺少系统码，请到willow注册当前系统码，注册格式为【%s:[系统码（3位）]】", willow));
    //     } else if (systemCode.length() != 3) {
    //         throw new RuntimeException(String.format("当前系统码长度不等于3位，请重新注册，注册格式为【%s:[系统码（3位）]】", willow));
    //     } else {
    //         code.append(httpStatusCode);
    //         code.append(systemCode);
    //         code.append(businessCode);
    //         return code.toString();
    //     }
    // }

    static {
        RESPONSE_CODE_PREFIX_MAPPED.put("sso", "002");
        RESPONSE_CODE_PREFIX_MAPPED.put("inf.uac.srv", "003");
        RESPONSE_CODE_PREFIX_MAPPED.put("avatar", "004");
        RESPONSE_CODE_PREFIX_MAPPED.put("pigeon", "005");
        RESPONSE_CODE_PREFIX_MAPPED.put("inf.pigeon.api", "006");
        RESPONSE_CODE_PREFIX_MAPPED.put("common.oss.srv", "101");
        RESPONSE_CODE_PREFIX_MAPPED.put("common.user.profile", "102");
        RESPONSE_CODE_PREFIX_MAPPED.put("common.sms.srv", "103");
        RESPONSE_CODE_PREFIX_MAPPED.put("common.audit.srv", "104");
        RESPONSE_CODE_PREFIX_MAPPED.put("common.etl.srv", "105");
        RESPONSE_CODE_PREFIX_MAPPED.put("common.route.srv", "106");
        RESPONSE_CODE_PREFIX_MAPPED.put("common.search.srv", "107");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.contract.api", "201");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.contract.srv", "202");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.customer.api", "203");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.customer.srv", "204");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.property.api", "205");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.property.srv", "206");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.settings.api", "207");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.settings.srv", "208");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.web.protal", "209");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.followup.srv", "210");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.merchandiser.srv", "211");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.finance.api", "212");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.finance.srv", "213");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.followup.api", "214");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.project.srv", "215");
        RESPONSE_CODE_PREFIX_MAPPED.put("erp.project.api", "216");
        RESPONSE_CODE_PREFIX_MAPPED.put("ops.community.api", "301");
        RESPONSE_CODE_PREFIX_MAPPED.put("ops.community.srv", "302");
        RESPONSE_CODE_PREFIX_MAPPED.put("ops.core.brand", "303");
        RESPONSE_CODE_PREFIX_MAPPED.put("ops.core.menu", "304");
        RESPONSE_CODE_PREFIX_MAPPED.put("ops.dict.srv", "305");
        RESPONSE_CODE_PREFIX_MAPPED.put("ops.gis.area", "306");
        RESPONSE_CODE_PREFIX_MAPPED.put("ops.web.api", "307");
        RESPONSE_CODE_PREFIX_MAPPED.put("ops.web.portal", "308");
        RESPONSE_CODE_PREFIX_MAPPED.put("ops.core.srv", "309");
        RESPONSE_CODE_PREFIX_MAPPED.put("open.core.srv", "401");
        RESPONSE_CODE_PREFIX_MAPPED.put("open.web.api", "402");
        RESPONSE_CODE_PREFIX_MAPPED.put("mall.core.srv", "501");
        RESPONSE_CODE_PREFIX_MAPPED.put("mall.web.api", "502");
    }
}

